//
//  HDLoginViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/12.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDLoginViewController.h"
#import "HDForgetPasswordViewController.h"
#import "HDRegisterViewController.h"

@interface HDLoginViewController ()
@property(nonatomic,strong)UIImageView *loginView;
@property(nonatomic,strong)UIView *inputView;
@property(nonatomic,strong)UITextField *accountField;
@property(nonatomic,strong)UITextField *passwordField;
@property(nonatomic,strong)GXButton *rememberPassword;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UIButton *registerBtn;
@property(nonatomic,strong)UIButton *forgetPasswordBtn;
@property CGPoint activePoint;
@end

@implementation HDLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44)];
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.loginView];
    [self.loginView addSubview:self.inputView];
    [self.loginView addSubview:self.rememberPassword];
    [self.loginView addSubview:self.loginBtn];
    [self.loginView addSubview:self.registerBtn];
    [self.loginView addSubview:self.forgetPasswordBtn];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.contentView addGestureRecognizer:tapGesture];
    [self registerForKeyboardNotifications];
    [self checkInputFinished];
}

- (void)checkInputFinished
{
    self.loginBtn.enabled = (self.accountField.text.length&&self.passwordField.text.length);
}

- (void)tapAction
{
    [self.view endEditing:YES];
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textBeginEditing:)
                                                name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(checkInputFinished)
                                                name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)textBeginEditing:(NSNotification*)aNotification
{
    UIView* sender=[aNotification object];
    CGRect rect=sender.frame;
    rect.origin.y+=rect.size.height;
    self.activePoint=[[sender superview] convertPoint:rect.origin toView:self.contentView];
    self.activePoint=[self.contentView convertPoint:self.activePoint toView:self.contentView];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    kbSize.height+=30;
    
    float hiddenH=self.contentView.frame.size.height-(self.contentView.frame.size.height-kbSize.height);
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,hiddenH, 0.0);
    self.contentView.contentInset = contentInsets;
    self.contentView.scrollIndicatorInsets = contentInsets;
    float activeH=self.contentView.frame.size.height-self.activePoint.y;
    if(kbSize.height>activeH)
    {
        float dif=activeH-kbSize.height;
        CGPoint scrollPoint = CGPointMake(0.0,0-dif);
        [self.contentView setContentOffset:scrollPoint animated:YES];
    }
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.3 animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.contentView.contentInset = contentInsets;
        self.contentView.scrollIndicatorInsets = contentInsets;
    }];
}

#pragma mark - actions
- (void)rememberPasswordAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
}

- (void)loginAction:(UIButton *)sender
{
    netEngine.account = self.accountField.text;
    if (self.rememberPassword.selected)
    {
        netEngine.password = self.passwordField.text;
    }
    else
    {
        netEngine.password = @"";
    }
    NSDictionary* params=@{
                           @"mobile_no":self.accountField.text,
                           @"password":self.passwordField.text,
                           };
    [netEngine RequestLoginWithTarget:self.VctrID hudView:self.contentView Data:params];
    
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"Login"]!=nil)
        {
            HDAppDelegate *appDelegate = (HDAppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate loadMainViewController];
        }
    }
}

- (void)registerAction:(UIButton *)sender
{
    HDRegisterViewController *registerVC = [[HDRegisterViewController alloc] init];
    registerVC.navigationItem.title = @"注册";
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)forgetPasswordAction:(UIButton *)sender
{
    HDForgetPasswordViewController *forgetPasswordVC = [[HDForgetPasswordViewController alloc] init];
    forgetPasswordVC.navigationItem.title = @"忘记密码";
    [self.navigationController pushViewController:forgetPasswordVC animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

#pragma mark - getters
- (UIImageView *)loginView
{
    if (!_loginView) {
        _loginView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, APPFRAME.size.width-20, (APPFRAME.size.width-20)*933/601.0)];
        _loginView.image = [UIImage imageNamed:@"loginBackgroundImage.png"];
        _loginView.layer.masksToBounds = YES;
        _loginView.userInteractionEnabled = YES;
        self.contentView.contentSize = CGSizeMake(APPFRAME.size.width, _loginView.height+20);
    }
    return _loginView;
}

- (UIView *)inputView
{
    if (!_inputView) {
        _inputView = [[UIView alloc] initWithFrame:CGRectMake(15, self.loginView.height/4.0+20, self.loginView.width-30, 88)];
        _inputView.layer.borderWidth = 1;
        _inputView.layer.borderColor = kGrayLineColor.CGColor;
        _inputView.layer.cornerRadius = 5;
        UIImageView *accountIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 14, 15, 16)];
        accountIcon.image = [UIImage imageNamed:@"账号.png"];
        [_inputView addSubview:accountIcon];
        UIImageView *passwordIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 58, 12, 15)];
        passwordIcon.image = [UIImage imageNamed:@"密码.png"];
        [_inputView addSubview:passwordIcon];
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(15, 44, _inputView.width-30, 0.5)];
        line.image = [UIImage imageNamed:@"直线.png"];
        [_inputView addSubview:line];
        [_inputView addSubview:self.accountField];
        [_inputView addSubview:self.passwordField];
    }
    return _inputView;
}

- (UITextField *)accountField
{
    if (!_accountField) {
        _accountField = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, self.loginView.width-75, 44)];
        _accountField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountField.placeholder = @"请输入登录账号";
        _accountField.font = [UIFont systemFontOfSize:14];
        _accountField.delegate = (id)self;
        if (netEngine.account.length)
        {
            _accountField.text = netEngine.account;
        }
    }
    return _accountField;
}

- (UITextField *)passwordField
{
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] initWithFrame:CGRectMake(40, 44, self.loginView.width-75, 44)];
        _passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordField.secureTextEntry = YES;
        _passwordField.placeholder = @"密码";
        _passwordField.delegate = (id)self;
        _passwordField.font = [UIFont systemFontOfSize:14];
        if (netEngine.password.length) {
            _passwordField.text = netEngine.password;
        }
    }
    return _passwordField;
}

- (GXButton *)rememberPassword
{
    if (!_rememberPassword) {
        _rememberPassword = [GXButton buttonWithType:UIButtonTypeCustom];
        _rememberPassword.frame = CGRectMake(15, self.inputView.bottom+10, 80, 30);
        _rememberPassword.imageViewFrame = CGRectMake(0, 7, 16, 16);
        _rememberPassword.titleLabelFrame = CGRectMake(25, 0, 55, 30);
        _rememberPassword.relayout = YES;
        [_rememberPassword setTitle:@"记住密码" forState:UIControlStateNormal];
        _rememberPassword.titleLabel.font = [UIFont systemFontOfSize:13];
        [_rememberPassword setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        [_rememberPassword setImage:[UIImage imageNamed:@"记住密码_未选中.png"] forState:UIControlStateNormal];
        [_rememberPassword setImage:[UIImage imageNamed:@"记住密码_选中.png"] forState:UIControlStateSelected];
        [_rememberPassword addTarget:self action:@selector(rememberPasswordAction:) forControlEvents:UIControlEventTouchUpInside];
        _rememberPassword.selected = netEngine.password.length;
    }
    return _rememberPassword;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(15, self.rememberPassword.bottom+15, self.loginView.width-30, 44);
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"登录.png"] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"确认登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.enabled = NO;
    }
    return _loginBtn;
}

- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerBtn.frame = CGRectMake(15, self.loginBtn.bottom+10, self.loginView.width-30, 44);
        [_registerBtn setBackgroundImage:[UIImage imageNamed:@"注册.png"] forState:UIControlStateNormal];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_registerBtn setTitleColor:KRedColor forState:UIControlStateNormal];
        [_registerBtn addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

- (UIButton *)forgetPasswordBtn
{
    if (!_forgetPasswordBtn) {
        _forgetPasswordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *btnTitle = @"忘记密码？别慌";
        CGSize size = [HDUtil getSizeWithText:btnTitle fontSize:14];
        _forgetPasswordBtn.frame = CGRectMake(self.loginView.width-15-size.width, self.registerBtn.bottom+5, size.width, 30);
        [_forgetPasswordBtn setTitle:btnTitle forState:UIControlStateNormal];
        _forgetPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_forgetPasswordBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
        [_forgetPasswordBtn addTarget:self action:@selector(forgetPasswordAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetPasswordBtn;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
